# -*- coding: utf-8 -*-
# @Author: zishuiliufeng
# @Date:   2021-02-12 16:14:57
# @Last Modified by:   zishuiliufeng
# @Last Modified time: 2021-02-16 17:11:35
import errno
import pymysql

#获取信息 condition=[['id',2],['status',0] ]    where id=2
def get_info(table,condition,orCondition=None,fields=None):
    if table is None or condition is None:
        return False

    try:
        db = pymysql.connect(host='127.0.0.1',user='root',passwd='zishuiliufeng',db='web',port=3306,charset='utf8')

        cursor = db.cursor()

        where = ''
        where_list = []
        if fields is None:
            if condition:
                for i in condition:
                    cstr = '%s=%s' %(i[0],i[1])
                    where_list.append(cstr)

                where = "{} {}".format("where",' and '.join(where_list))

            

            sql = "select * from `%s` %s" %(table,where)
        
        
        # 执行sql语句
        cursor.execute(sql)

        res = cursor.fetchall()
        news_res = [dict(zip([x[0] for x in cursor.description],row)) for row in res]
        #print(news_res)
        

    except pymysql.Error as e:
        print(e)
        print('操作数据库失败')
    finally:
        # 如果连接成功就要关闭数据库
        if db:
            db.close()

    return news_res

#写入txt文本 
#file:文本地址 content:内容  mode:写入的方式  a追加 w覆盖...
def writeToTxt(file,content,mode = "a"):
    with open(file,mode,encoding='utf-8') as fo:
        fo.write(content+'\n')  #换行
        #fo.writelines(content) 
    fo.close()
    return True